﻿using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Media;

using SBPweb.RIA.Client.Controls.Base;

namespace SBPweb.RIA.Client.Controls.Content
{

    public partial class HtmlDiv : UserControlBase
    {

        #region Fields

        protected HtmlElement container;
        private HtmlElement divParent;

        #endregion

        #region Constructor logic

        public HtmlDiv(HtmlElement parent)
        {
            IsLayoutAnchored = true;
            divParent = parent;
            InstanceCount++;
            InitializeComponent();
            InitializeControls();
            InitializeHandlers();
        }

        public HtmlDiv()
            : this(ExtendableApplication.Current.IsRunningOutOfBrowser ? null : HtmlPage.Plugin.Parent)
        {
        }

        #endregion

        #region Dependencies

        public static readonly DependencyProperty InnerHtmlProperty = DependencyProperty.Register("InnerHtml", typeof(string), typeof(HtmlDiv), null);
        public static readonly DependencyProperty ZIndexProperty = DependencyProperty.Register("ZIndex", typeof(int), typeof(HtmlDiv), null);

        #endregion

        #region Properties

        private static int InstanceCount
        {
            get;
            set;
        }

        public string InnerHtml
        {
            get
            {
                return base.GetValue<string>(InnerHtmlProperty);
            }
            set
            {
                SetValue<string>(InnerHtmlProperty, value, "InnerHtml");
                container.SetProperty("innerHtml", value);
            }
        }

        public int ZIndex
        {
            get
            {
                return base.GetValue<int>(ZIndexProperty);
            }
            set
            {
                SetValue<int>(ZIndexProperty, value, "ZIndex");
                if (!ExtendableApplication.Current.IsRunningOutOfBrowser)
                {
                    container.SetStyleAttribute("z-index", value.ToString());
                }
                else
                {
                    //browser.
                }
            }
        }

        public bool IsLayoutAnchored
        {
            get;
            set;
        }

        public string ContainerId
        {
            get
            {
                return container.Id;
            }
            set
            {
                container.Id = value;
            }
        }

        #endregion

        #region Methods

        //protected virtual string GetInnerElementTagName()
        //{
        //    return "div";
        //}

        //protected virtual void InitializeInnerElementTag(HtmlElement element)
        //{
        //}

        protected virtual HtmlElement GetInnerElement()
        {
            return null;
        }

        protected virtual string GetContainerId()
        {
            return string.Format("_SL4_htmlFrame_{0}", InstanceCount);
        }

        protected virtual void InitializeControls()
        {
            if (ExtendableApplication.Current.IsDesignTime)
            {
                return;
            }
            if (!ExtendableApplication.Current.IsRunningOutOfBrowser)
            {
                container = HtmlPage.Document.CreateElement("div");
                container.Id = GetContainerId();
                container.SetStyleAttribute("position", "absolute");
                ZIndex = InstanceCount + 10000;

                HtmlElement element = GetInnerElement();
                if (element != null)
                {
                    container.AppendChild(element);
                }

                divParent.AppendChild(container);
            }
        }

        protected void InitializeHandlers()
        {
            this.SizeChanged += new SizeChangedEventHandler(OnSizeChanged);
            this.LayoutUpdated += new EventHandler(OnLayoutUpdated);
        }

        #endregion

        #region Event handlers

        public void ResizeContainer(Size size)
        {
            if (!ExtendableApplication.Current.IsRunningOutOfBrowser)
            {
                container.SetStyleAttribute("width", string.Format("{0}px", size.Width));
                container.SetStyleAttribute("height", string.Format("{0}px", size.Height));
            }
            else
            {
            }
        }

        public void MoveContainer(Point offset)
        {
            if (!ExtendableApplication.Current.IsRunningOutOfBrowser)
            {
                int bodyLeft = Convert.ToInt32(HtmlPage.Plugin.Parent.GetAttribute("offsetLeft"));
                int bodyTop = Convert.ToInt32(HtmlPage.Plugin.Parent.GetAttribute("offsetTop"));
                container.SetStyleAttribute("left", string.Format("{0}px", bodyLeft + offset.X));
                container.SetStyleAttribute("top", string.Format("{0}px", bodyTop + offset.Y));
            }
            else
            {
            }
        }

        protected virtual void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (IsLayoutAnchored)
            {
                ResizeContainer(e.NewSize);
            }
        }

        protected virtual void OnLayoutUpdated(object sender, EventArgs e)
        {
            if (IsLayoutAnchored)
            {
                GeneralTransform gt = this.TransformToVisual(ExtendableApplication.Current.RootVisual);
                Point offset = gt.Transform(new Point(0, 0));
                MoveContainer(offset);
            }
        }

        #endregion

    }

}